﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using ClassLibrary.DAO;
using ClassLibrary.Data;
using System.Collections.Generic;

public partial class UserControl_ucHowtobuy : System.Web.UI.UserControl
{
    TblChipDAO chipDAO = new TblChipDAO(Global.CONNECTION_STRING);

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            LoadChip();
        }
    }

    private void LoadChip()
    {
        try
        {
            grdChip.DataSource = chipDAO.GetChipList();
            grdChip.DataBind();
        }
        catch (Exception ex)
        {
            Session[Global.SESSION_ERROR] = ex;
            Response.Redirect(Global.ERROR_PAGE);
        }
    }

 
    protected void btnMua_Click1(object sender, EventArgs e)
    {
        bool flag = false;
        try
        {
            //string IdItem = ((ImageButton)sender).CommandArgument;
            TblChip chip = new TblChip();
            chip.IDChip = Convert.ToInt32(((Button)sender).CommandArgument);

            int IdItem = chip.IDChip;


            BasePage.SetShoppingCartSesstion(IdItem + "|0");

            List<TblBookingDetail> arrItems = new List<TblBookingDetail>();
            if (Session[Global.SESSION_CART] != null)
                arrItems = (List<TblBookingDetail>)Session[Global.SESSION_CART];

            int iNoOfItems = 0;
            double dTotalAmount = 0;

            foreach (TblBookingDetail order in arrItems)
            {
                iNoOfItems += order.Quantity;
                dTotalAmount += Convert.ToDouble(order.Quantity) * Convert.ToDouble(order.Price);
            }

            flag = true;
            Session[Global.SESSION_TOTAL] = dTotalAmount;
            Session[Global.SESSION_CART] = arrItems;

        }
        catch (Exception ex)
        {
            Session[Global.SESSION_ERROR] = ex;
            Response.Redirect(Global.ERROR_PAGE, true);
        }
        if (flag)
            Response.Redirect("~/MuaHang.aspx");
    }
}
